from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin


class AuthMiddleware(MiddlewareMixin):

    def process_request(self, request):
        # 排除不需要验证的页面
        if request.path in ['/user/login/','/user/register/','/user/image/code/']:
            return None
        # 检查用户是否已登录
        info_dict = request.session.get('info')
        if info_dict:
            return
        # 如果用户未登录，重定向到登录页面
        return redirect('user:login')
